home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Fast HTML 3151710272001.psc / commentedForm2.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-27  |  6.9 KB  |  136 lines

  1. VERSION 5.00
  2. Begin VB.Form commentedForm1 
  3.    BackColor       =   &H00808080&
  4.    Caption         =   "Fast HTML Highlight3"
  5.    ClientHeight    =   2175
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   4470
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   2175
  11.    ScaleWidth      =   4470
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.Label Label2 
  14.       Caption         =   "This form just contains Heavily commented Code"
  15.       Height          =   495
  16.       Left            =   120
  17.       TabIndex        =   1
  18.       Top             =   1200
  19.       Width           =   3855
  20.    End
  21.    Begin VB.Label Label1 
  22.       Caption         =   "Look at the code"
  23.       BeginProperty Font 
  24.          Name            =   "MS Sans Serif"
  25.          Size            =   24
  26.          Charset         =   0
  27.          Weight          =   400
  28.          Underline       =   0   'False
  29.          Italic          =   0   'False
  30.          Strikethrough   =   0   'False
  31.       EndProperty
  32.       Height          =   735
  33.       Left            =   120
  34.       TabIndex        =   0
  35.       Top             =   240
  36.       Width           =   3855
  37.    End
  38. Attribute VB_Name = "commentedForm1"
  39. Attribute VB_GlobalNameSpace = False
  40. Attribute VB_Creatable = False
  41. Attribute VB_PredeclaredId = True
  42. Attribute VB_Exposed = False
  43. ' This is just a Heavily commented version of
  44. ' the colorhtml function in Form1
  45. ' This is meant to help understand regular expresions
  46. ' Needs reference to Microsoft VBscript Regular Expressions.
  47. ' Get it at http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/msdn-files/027/001/733/msdncompositedoc.xml
  48. ' You should download the documentaion as well
  49. Function colorhtml()
  50. '-----------------------------------------------
  51. 'Define Regularexpressions for colorize function
  52. '-----------------------------------------------
  53. 'Regular expressions allow you to search and replace strings using complex patterns
  54. 'This is great for HTML since there are loose standards
  55. 'for example <font size="1"> and <font  size =  "1"> and <font size=1> are all valid
  56. 'This makes HTML difficult to search properly, but with regular expresion it is easy.
  57. 'regx for Tags
  58.     Dim TagregEx, Match, Matches   ' Create variable.
  59.     Set TagregEx = New RegExp      ' Create a regular expression.
  60.     TagregEx.Pattern = "<(.)[^> ]*( ){0,1}[^>]*>"   ' Set pattern.
  61.     ' ok you are probaly wondering what <(.)[^> ]+( )*[^>]+> is.
  62.     ' this is my regular expression to find HTML tags
  63.     ' It basically says match any group of characters begining with
  64.     ' < followed immediatly by any char . which is in parenthesis telling the regx engine
  65.     ' to remember this value for later. - I will use this to see if our tag is a comment or end tag
  66.     ' then we look for [^> ] brackets define a character class, and the ^ means not
  67.     ' this is followed by a * wich means match [^> ] 0 or more times
  68.     ' so [^> ]* means a group of characters that doesn't contain a > or a space
  69.     ' then we have ( ){0,1} match 0 or 1 spaces and store it for later
  70.     ' are you getting the hang of this yet?
  71.     ' last is [^>]*> match any number of characters that isn't a > followed by a >
  72.     ' This seems complex but really it isnt.
  73.     ' A simple tag regx could be written as "<[^>]+>" the + means match one or more times
  74.     ' Mine is more complex, because remembering the first character and
  75.     ' if the tag contained a space after the name
  76.     TagregEx.IgnoreCase = False    ' Set case insensitivity. We aren't looking for any chars so no need.
  77.     TagregEx.Global = True         ' Set global applicability. Global just means match the pattern as many times as possible
  78. 'regx for property="value" pairs
  79.     Dim tagPNregEx, Match2, Matches2    ' Create variable.
  80.     Set tagPNregEx = New RegExp         ' Create a regular expression.
  81.     tagPNregEx.Pattern = "(\w+ *=) *(\d+|""[^""]+"")"   ' tag propertyname.
  82.     ' again a funny looking line of jibberish.
  83.     ' all that is new here is \w (any word character) and \d (any digit) and | wich means or
  84.     ' so now that you know that you know this jibberish says
  85.     ' match a group of word characters followed by 0 or more spaces followed by an = sign
  86.     ' followed by 0 or more spaces followed by either a group of digits or
  87.     ' an equal sign followed by anything that isnt an equal sign followed by another equal sign
  88.     ' also we are remembering up to including the equal sign and every thing after the equal sign
  89.     ' what a mouthfull I sure love the fact that I can write all that as "(\w+ *=) *(\d+|""[^""]+"")"
  90.     tagPNregEx.IgnoreCase = False       ' Set case insensitivity.
  91.     tagPNregEx.Global = True            ' Set global applicability.
  92. '---------------------------------------------
  93. Dim rtfstart As Long
  94. rtfstart = rtf1.SelStart ' Remember startpos since user might have selected text
  95. If rtf1.SelLength < 1 Then
  96.     MsgBox "No text selected"
  97. Exit Function
  98. End If
  99. '----------------------------------------------
  100.     Set Matches = TagregEx.Execute(rtf1.SelText)    ' Execute search.
  101.     For Each Match In Matches     ' Iterate Matches collection.
  102.         If Match.Value <> "" Then 'used to stop empty string match return
  103.             rtf1.SelStart = rtfstart + Match.FirstIndex
  104.             rtf1.SelLength = Match.Length
  105.             rtf1.SelColor = color(0)
  106.             ' now run some short circuit logic so we only run second regx if we have to.
  107.             ' since our tag regx said remember the first char, we will have it in our submatches collection
  108.             If Match.SubMatches(0) = "!" Then ' looks like a comment
  109.                rtf1.SelColor = color(3)
  110.                GoTo nextmatch
  111.             ElseIf Match.SubMatches(1) <> " " Then ' this tag doesn't have properties
  112.                 GoTo nextmatch
  113.             End If
  114.             Set Matches2 = tagPNregEx.Execute(Match.Value) ' Execute search.
  115.             ' This would also work as tagPNregEx.Execute (rtf1.SelText)
  116.             ' But since our matches collection has this information it
  117.             ' is faster and cleaner to use the matches collection information
  118.             ' notice, that the only time we need to reference the rtf1.sel attributes
  119.             ' is when we make a color change.
  120.             ' This is what slows everything down, not the regular expressions.
  121.             For Each Match2 In Matches2
  122.                 If Match2.Value <> "" Then 'used to stop empty string match return
  123.                     'Debug.Print Match2.Value & Match2.Length
  124.                     rtf1.SelStart = Match.FirstIndex + rtfstart + Match2.FirstIndex
  125.                     rtf1.SelLength = Match2.Length
  126.                     rtf1.SelColor = color(2)
  127.                     rtf1.SelLength = Len(Match2.SubMatches(0))
  128.                     rtf1.SelColor = color(1)
  129.                 End If
  130.             Next
  131.         End If
  132. nextmatch:
  133.     Next
  134.     lbltagcount.Caption = Matches.Count & " Tags"
  135. End Function
  136.